// components/info-nav-btn/info-nav-btn.js
const app = getApp();
import Http from '../../utils/http.js';
const http = new Http();
Component({
    /**
     * 组件的属性列表
     */
    properties: {
        country: String,
        city: String,
    },

    /**
     * 组件的初始数据
     */
    data: {
        num: 0,
    },
    observers: {
        //
        // == data[0].id
        country: function (country) {
            if (country) {
                this.getMapPointCnt();
            }
        },
    },

    pageLifetimes: {
        show: function () {
            // 页面被展示
            if (wx.getStorageSync('mapAddressInfo')) {
                this.getMapPointCnt();
                let addressInfo = wx.getStorageSync('mapAddressInfo');
                app.globalData.countryBack = addressInfo.country;
                app.globalData.addressBack = addressInfo.city;
                app.globalData.cityLat = addressInfo.cityLat;
                app.globalData.cityLng = addressInfo.cityLng;
                wx.removeStorageSync('mapAddressInfo');
            }
        },
    },
    /**
     * 组件的方法列表
     */
    methods: {
        jumpTap(e) {
            let addressInfo = {
                country: app.globalData.countryBack,
                city: app.globalData.addressBack,
                cityLat: app.globalData.cityLat,
                cityLng: app.globalData.cityLng,
            };
            wx.setStorageSync('mapAddressInfo', addressInfo);
            app.globalData.countryBack = this.data.country;
            app.globalData.addressBack = this.data.city;
            wx.navigateTo({
                url: e.currentTarget.dataset.url,
            });
        },
        //获取类型数量
        getMapPointCnt: function (e) {
            let _that = this;
            let region;
            if (_that.data.country == '中国') {
                if (_that.data.city == '香港') {
                    region = '中国香港';
                } else if (app.globalData.taiwan.includes(_that.data.city)) {
                    region = '中国台湾';
                } else if (_that.data.city == '澳门') {
                    region = '中国澳门';
                } else {
                    region = '中国大陆';
                }
            } else {
                region = _that.data.country;
            }

            http.getRequest(
                `code/getMapPointCnt?city=${_that.data.city}&country=${_that.data.country}&region=${region}`
            ).then((res) => {
                if (res.status == 0) {
                    let allNum = 0;
                    for (let i = 0; i < res.data.length; i++) {
                        allNum += res.data[i].num;
                    }
                    _that.setData({
                        num: allNum,
                    });
                }
            });
        },
    },
});
